package com.copsoftware.struts.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;


import com.copsoftware.mantenimientos.testigo.delegate.TestigoDelegate;
import com.copsoftware.procesos.investigacion.delegate.InvestigacionDelegate;
import com.copsoftware.struts.forms.BaseActionForm;
import com.copsoftware.struts.forms.RegistrarTestigoInvestigacionForm;
   
public class RegistrarTestigoInvestigacionAction extends BaseAction {

	@Override
	public ActionForward process(ActionMapping mapping,
			BaseActionForm baseForm, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		RegistrarTestigoInvestigacionForm form = (RegistrarTestigoInvestigacionForm) baseForm;
		ActionForward nextPage = mapping.findForward("EXITO_JSP");		
		
		TestigoDelegate testigoDelegate = new TestigoDelegate();
		InvestigacionDelegate investigacionDelegate = new InvestigacionDelegate();
		
		

			
		if (form.getBtnRegistrarTestigo().isSelected())
		{
			//Se registra al testigo si es nuevo
			testigoDelegate.guardarTestigo(form);
								
			//Se asocia al testigo a la investigacion
			if (!form.getCPersona().equals("") && (!form.getCInvestigacion().equals("")))
			{
				investigacionDelegate.guardarTestigoInvestigacion(form);
				nextPage = mapping.findForward("EXITO_JSP");	
			}
		}
		
		if ((form != null) && (!form.getCInvestigacion().equals("")))
		{
			if (form.getCPersona() != null)
			{
				testigoDelegate.buscarTestigo(form);
				investigacionDelegate.buscarTestigo(form);
			}
				//SE DEVUELVE EL FORM
			nextPage = mapping.findForward("MOSTRAR_TESTIGO");
		}
		
		//form.setCampoDni(new CampoVista("42362707","txt"));
		
		request.setAttribute("RegistrarTestigoInvestigacionForm",
				form);
		
		return nextPage;
	}
}
